package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.annotation.OperateLogAnnotation;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Override
    public PageBean selectStudent(StudentQueryParam studentQueryParam) {

        System.out.println("studentQueryParam = " + studentQueryParam);

        PageHelper.startPage(studentQueryParam.getPage(),studentQueryParam.getPageSize());

        List<Student> list =  studentMapper.selectStudent(studentQueryParam);
        Page newList = (Page) list;

        PageBean pageObj = new PageBean(newList.getTotal(),newList.getResult());


        return pageObj;
    }

    @Override
    @OperateLogAnnotation
    @Transactional(rollbackFor = Exception.class)
    public void delStudentByIds(List<Integer> ids) {

        studentMapper.delStudentByIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Student> selectStudentByClazzId(Integer id) {
        System.out.println("id222 = " + id);
       List<Student> list = studentMapper.selectStudentByClazzId(id);
        return list;
    }

    @Override
    @OperateLogAnnotation
    @Transactional(rollbackFor = Exception.class)
    public void insertStudent(Student student) {

        // student.setViolationCount((short)0);
        // student.setViolationScore((short)0);

        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insertStudent(student);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Student getStudentById(Integer id) {
        Student student =  studentMapper.getStudentById(id);
        return student;
    }

    @Override
    @OperateLogAnnotation
    @Transactional(rollbackFor = Exception.class)
    public void updateStudent(Student student) {
        student.setUpdateTime(LocalDateTime.now());

        studentMapper.updateStudent(student);
    }

    @Override
    @OperateLogAnnotation
    @Transactional(rollbackFor = Exception.class)
    public void updateViolation(Integer id, Integer score) {
        studentMapper.updateViolation(id,score);
    }
}
